package com.bpms.common;

import java.util.Enumeration;

import com.bpms.common.VO;

public class testJworker {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
//		VO updateVO = new VO();
//		updateVO.setAttr("ziduan1", "ziduan1value1");
//		updateVO.setAttr("ziduan2", "ziduan1value2");
//		updateVO.setAttr("ziduan3", "ziduan1value3");
//		updateVO.setAttr("ziduan4", "ziduan1value4");
//		updateVO.setAttr("key1", "key1value");
//		updateVO.setAttr("key2", "key1value");		
//		updateVO.keyString = "key1,key2";
//		updateVO.voType = "table_name";
		String sql= "0000000000000000";
//		String eSql = (new StringBuilder(String.valueOf(prepareUpdate(updateVO, updateVO.getKeyVO())))).append(sql).toString();
//		System.out.println("eSql = " + eSql);

		VO updateVO = new VO();
		updateVO.setAttr("ziduan1", "ziduan1value1");
		updateVO.setAttr("ziduan2", "ziduan1value2");
		updateVO.setAttr("ziduan3", "ziduan1value3");
		updateVO.setAttr("ziduan4", "ziduan1value4");
		VO keyVO = new VO();
		keyVO.setAttr("key1", "key1value1");
		keyVO.setAttr("key1", "key1value2");
		keyVO.voType = "tablename";
		keyVO.keyString="key1,key2,";
		//JdbcWorker j = new JdbcWorker();
		String eSql = (new StringBuilder(String.valueOf(prepareUpdate(updateVO, keyVO)))).append(sql).toString();
		System.out.println("eSql = " + eSql);
	}

	public static String prepareUpdate(VO updateVO, VO keyVO) {
		String tableName = keyVO.voType;
		String columnName = "";
		String value = "";
		String sqlString = (new StringBuilder("update ")).append(tableName).append(" set ").toString();
		Enumeration columns = updateVO.attributes.keys();
		if (updateVO.keyString == null)
			updateVO.keyString = keyVO.keyString;
		String keyS = (new StringBuilder(",")).append(updateVO.keyString.toUpperCase()).append(",").toString();
		while (columns.hasMoreElements()) {
			columnName = (String) columns.nextElement();
			String he = columnName.toUpperCase();
			sqlString = (new StringBuilder(String.valueOf(sqlString))).append(columnName).append("=").append("?").append(",").toString();
		}
		if (sqlString.lastIndexOf(",") > -1)
			sqlString = sqlString.substring(0, sqlString.lastIndexOf(","));
		sqlString = (new StringBuilder(String.valueOf(sqlString))).append(" where ").toString();
		for (Enumeration columns1 = keyVO.attributes.keys(); columns1.hasMoreElements();) {
			columnName = (String) columns1.nextElement();
			sqlString = (new StringBuilder(String.valueOf(sqlString))).append(columnName).append("=").append("?").append(" and ").toString();
		}

		if (sqlString.lastIndexOf("and") > -1)
			sqlString = sqlString.substring(0, sqlString.lastIndexOf("and"));
		return sqlString;
	}
}
